package com.be.beadmin.project.controller;

import com.alibaba.excel.EasyExcel;
import com.be.beadmin.common.entity.PageEntity;
import com.be.beadmin.common.utils.ResultModel;
import com.be.beadmin.project.dto.input.PlanStageDto;
import com.be.beadmin.project.dto.output.PlanStageEx;
import com.be.beadmin.project.service.PlanStageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;

/**
* @author zjm
* @date 2023-06-18
**/
@RestController
public class PlanStageController {

    @Autowired
    private  PlanStageService planStageService;


    @PostMapping(value = "/design/getPlanStageByPage")
    public ResultModel getPlanStageByPage(@RequestBody PageEntity pageEntity)  {
        return ResultModel.success(planStageService.getPlanStageByPage(pageEntity)) ;
    }

    @PostMapping("/design/addPlanStage")
    public ResultModel addPlanStage(@RequestBody PlanStageDto planStageDto){
        planStageService.addPlanStage(planStageDto);
        return ResultModel.success(ResultModel.ADD);
    }

    @PostMapping("/design/delPlanStage")
    public ResultModel delPlanStage(@RequestBody PlanStageDto planStageDto){
        planStageService.delPlanStage(planStageDto.getId());
        return ResultModel.success(ResultModel.DELETE);
    }


    @PostMapping("/design/updatePlanStage")
    public ResultModel updatePlanStage(@RequestBody PlanStageDto planStageDto){
        planStageService.updatePlanStage(planStageDto);
        return ResultModel.success(ResultModel.UPDATE);
    }


    @PostMapping("/design/exportPlanStage")
    public void exportPlanStage(HttpServletResponse response ,@RequestBody PageEntity pageEntity) throws IOException {
        Page<PlanStageEx> planStagePage = planStageService.getPlanStageByPage(pageEntity);
        List<PlanStageEx> planStageExes = planStagePage.getContent();
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("PlanStage接口", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        EasyExcel.write(response.getOutputStream(), PlanStageEx.class).sheet("sheet1").doWrite(planStageExes);
    }
}